package com.whut.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.whut.model.User;
import com.whut.service.UserManager;
import com.whut.util.UtilCommon;
import com.whut.vo.UserLoginInfo;

public class LoginAction extends ActionSupport implements ModelDriven {

	private UserManager um;
	private List<User> users;
	private User user;
	User u = new User();
	private UserLoginInfo userInfo = new UserLoginInfo();	 
	private String result;
	private HashMap<String, Object> jMap;  

	public LoginAction() {  
		//初始化Map对象  
		jMap = new HashMap<String, Object>();  
    }  

	@Override
	public String execute() {
		// TODO Auto-generated method stub
		System.out.println(userInfo.getUsername());
		System.out.println(userInfo.getPassword());
		jMap.clear();
		u.setUsername(userInfo.getUsername());
		u.setPassword(UtilCommon.md5Encryption(userInfo.getPassword()));
		
 		JSONObject jsonObj = new JSONObject();
 		jsonObj.put("username",u.getUsername());
		
		try {
			
			if(um.exists(u)){
				if (um.legalUser(u)) {	      		  
			        // 如果用户名与密码有效即返回成功				
					jsonObj.put("success",true);
//					jMap.put("success",true);
			         		        
			       
			        
			     // 调用json对象的toString方法转换为字符串然后赋值给result 
				  	result = jsonObj.toString();
					// 可以测试一下result  
			       
			     //    u.setLoginTimes(u,u.getUsername());
//			        um.addLoginTimes(u);
					
				} else{
					System.out.println("Wrong");
					// 密码无效即返回失败				
 					jsonObj.put("success",false);
 					jsonObj.put("msg","密码错误");
 					result = jsonObj.toString();
//					jMap.put("success",false);
//					jMap.put("msg","密码错误");
					// 可以测试一下result  
			       
			         
				}
			} 	else {
				System.out.println("Wrong");
				// 如果用户名无效即返回失败				
 				jsonObj.put("success",false);
 				jsonObj.put("msg","用户名未注册");
 				result = jsonObj.toString();
//				jMap.put("success",false);
//				jMap.put("msg","用户名未注册");
				// 可以测试一下result  
		        
		        
			}  
 			System.out.println(result); 
//			UtilCommon.writeJson(jMap); 
			return "success";
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
	}

 
 
	public String execute2() {
		// TODO Auto-generated method stub
		System.out.println(userInfo.getUsername());
		System.out.println(userInfo.getPassword());
		jMap.clear();
		u.setUsername(userInfo.getUsername());
		u.setPassword(userInfo.getPassword());		
		System.out.println(u.getPassword());
		try {
			
			if(um.exists(u)){
				if (um.legalUser(u)) {	      		  
			        // 如果用户名与密码有效即返回成功							
					jMap.put("success",true);  	    
					
				} else{
					System.out.println("Wrong");
					// 密码无效即返回失败		 					
					jMap.put("success",false);
					jMap.put("msg","密码错误");					
				}
			} 	else {
				System.out.println("Wrong");
				// 如果用户名无效即返回失败		 			
				jMap.put("success",false);
				jMap.put("msg","用户名未注册");
			}  
 			
			UtilCommon.writeJson(jMap); 
			return null;
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error");
			return "exception";
		}
	}	
 
	
	public UserManager getUm() {
		return um;
	}

	//@Resource(name = "userManager")
	public void setUm(UserManager um) {
		this.um = um;
	}

	// @Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return userInfo;
	}
			
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserLoginInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserLoginInfo userInfo) {
		this.userInfo = userInfo;
	}

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public HashMap<String, Object> getjMap() {
		return jMap;
	}

	public void setjMap(HashMap<String, Object> jMap) {
		this.jMap = jMap;
	}	 

}
